package edu.wpi.cs509.representation;

import static org.junit.Assert.fail;

import java.util.logging.Logger;

import org.junit.Test;

public class ActionTest0 {

	Game game;  // the game
	Gameboard gb; // the gameboard
	Location loc; // a location
	PlayerPiece piece, piece2; // two pieces

	ActionAdd aAdd; // an add action
	ActionMove aMove; // a move action
	ActionRemove aRemove; // a remove action


	/**
	 * The main test routine.  It goes through each piece and checks the characteristics.
	 */
	@Test(timeout=5000)
	public void testTheAction() {
		int xCoord = 9;
		int yCoord = 0;
		boolean isRed = (yCoord <=4);

		// generate game, gameboard
		game = new Game();
		gb = game.getGameboard();

		////////////////////
		// add a marshal
		Logger.getLogger("global").info("--- add a marshal");
		piece = gb.getPlayerPieceInBoneyard(
				isRed ? TeamColor.RED : TeamColor.BLUE,
						StrategoPiece.MARSHAL);
		try {
			loc = gb.getBoardsquare(xCoord, yCoord);
		} catch (GameboardException e) {
			e.printStackTrace();
			fail("100.0 - caught an exception");
		}

		// do what the Client would do (check the move)
		aAdd = new ActionAdd("add a Marshal", gb,piece,loc);
		try {
			aAdd.strategoRulesCheck();
		} catch (ActionNotLegalException e) {
			e.printStackTrace();
			fail("101.2 - caught an exception");

		}

		// do what the Server would do (check the move, then execute() to update the gameboard)
		try {
			aAdd.execute();
		} catch (ActionEndOfGameException e) {
			e.printStackTrace();
			fail("101.1 - caught an exception");
		} catch (ActionNotLegalException e) {
			e.printStackTrace();
			fail("101.2 - caught an exception");

		}

		///////////////////////
		// check that it ended up at the right location
		Logger.getLogger("global").info("--- check that it ended up the right location");
		try {
			piece2 = gb.getPlayerPieceOnBoardSquare(xCoord, yCoord);
		} catch (GameboardException e) {
			e.printStackTrace();
			fail("100.2 - caught an exception");

		}

		if (piece != piece2)
			fail("100.3 - piece did not get added as expected");


		////////////////////
		// move the piece on the board

		Logger.getLogger("global").info("--- move the piece on the board");
		try {
			piece = gb.getPlayerPieceOnBoardSquare(xCoord, yCoord);
		} catch (GameboardException e1) {
			e1.printStackTrace();
			fail("101.0 - caught an exception");
		}

		// change to a new location
		if (yCoord <= 3)
			yCoord++;
		if (yCoord >= 6)
			yCoord--;

		try {
			loc = gb.getBoardsquare(xCoord, yCoord);
		} catch (GameboardException e) {
			e.printStackTrace();
			fail("101.2 - caught an exception");
		}

		aMove = new ActionMove("move Marshal", gb,piece,loc);
		try {
			aMove.execute();
		} catch (ActionNotLegalException e) {
			e.printStackTrace();
			fail("101.3 - caught an exception");
		} catch (ActionEndOfGameException e) {
			e.printStackTrace();
			fail("101.3 - caught an exception - end of game is unexpected");
		}

		///////////////////////
		// check that it ended up at the right location
		Logger.getLogger("global").info("--- check that it ended up the right location");
		try {
			piece2 = gb.getPlayerPieceOnBoardSquare(xCoord, yCoord);
		} catch (GameboardException e) {
			e.printStackTrace();
			fail("101.4 - caught an exception");
		}

		if (piece==null)
			Logger.getLogger("global").info("piece=null");
		Logger.getLogger("global").info("piece.Y  " +piece.getLocation().getYCoordinate()+ " X "+piece.getLocation().getXCoordinate());
		if (piece2==null)
			Logger.getLogger("global").info("piece2=null");
		Logger.getLogger("global").info("piece2.Y "+piece2.getLocation().getYCoordinate()+" X "+piece2.getLocation().getXCoordinate());

		if (piece != piece2)
			fail("101.5 - piece did not get moved as expected");


		////////////////////
		// remove the piece from the board
		Logger.getLogger("global").info("remove the piece from the board");
		try {
			piece = gb.getPlayerPieceOnBoardSquare(xCoord, yCoord);
		} catch (GameboardException e1) {
			e1.printStackTrace();
			fail("102.0 - caught an exception");
		}


		loc = gb.getBoneyard();


		aRemove = new ActionRemove("remove Marshal", gb,piece,loc);
		try {
			aRemove.execute();
		} catch (ActionNotLegalException e) {
			e.printStackTrace();
			fail("102.2 - caught an exception");
		} catch (ActionEndOfGameException e) {
			e.printStackTrace();
			fail("102.2 - caught an exception");
		}

		///////////////////////
		// check that it ended up in the boneyard
		Logger.getLogger("global").info("check that it ended up in the boneyard");
		piece2 = gb.getPlayerPieceInBoneyard(
				isRed ? TeamColor.RED : TeamColor.BLUE,
						StrategoPiece.MARSHAL);
		if (piece != piece2)
			fail("102.3 - piece did not get removed as expected");



		Logger.getLogger("global").info("--------- next test ---------");



		Logger.getLogger("global").info("done");



	}
}
